package com.example.mallproduct.service.impl;

import com.example.mallproduct.api.domain.Product;
import com.example.mallproduct.api.dao.ProductDao;
import com.example.mallproduct.api.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/** @author yangtao */
@Service
public class ProductServiceImpl implements ProductService {
  @Autowired private ProductDao productDao;

  @Override
  public Product findByPid(Integer pid) {
    return productDao.selectById(pid);
  }

  @Override
  public void reduceInventory(Integer pid, int number) {
    Product product = productDao.selectById(pid);
    if (product.getStock() < number) {
      throw new RuntimeException("库存不足");
    }
    //int i = 1 / 0;
    product.setStock(product.getStock() - number);
    productDao.updateById(product);
  }
}
